package com.softer.chengxin.home.service.impl;

import com.softer.chengxin.common.domin.*;
import com.softer.chengxin.common.enums.InfoTypeEnum;
import com.softer.chengxin.common.enums.NumberEnum;
import com.softer.chengxin.corp.domin.Corp;
import com.softer.chengxin.home.dao.HomeDao;
import com.softer.chengxin.home.domin.Column;
import com.softer.chengxin.home.domin.Pie;
import com.softer.chengxin.home.service.HomeService;
import com.softer.chengxin.score.dao.SDManagerDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author Gong Zhiyang
 * @date 2018/9/26
 */
@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    private HomeDao homeDao;
    @Autowired
    private SDManagerDao managerDao;

    /**
     * @param userName  登陆用户名
     * @param loginType 登陆类型（1:物业企业，0:管理部门）
     * @param projectId 项目ID
     * @return 返回菜单列表
     */
    @Override
    public List<Module> selectAll(String userName, String loginType, String projectId) {
        String modulesId = "";
        if (Common.ONE.equals(loginType)) {
            //数据库中ID为1存的是物业企业菜单
            if (StringUtils.isNotBlank(projectId)) {
                modulesId = homeDao.selectModulesIdById(String.valueOf(NumberEnum.TWO.getNum()));
            } else {
                modulesId = homeDao.selectModulesIdById(Common.ONE);
            }
        } else if (Common.ZERO.equals(loginType)) {
            //获取权限id
            modulesId = homeDao.selectModulesId(userName);
        }
        if (StringUtils.isBlank(modulesId)) {
            return new ArrayList<>();
        }
        return homeDao.selectModules(modulesId);
    }

    /**
     * 查询待办事项数量
     *
     * @param id        账号ID
     * @param loginName 账号名
     * @return 数量结合
     */
    @Override
    public Map<String, Object> findCountByState(int id, String loginName) {
        //查询登陆账号的管理区域
        String cropId = homeDao.findAreaId(id);
        //查询账号是区管理部门还是市管理部门3市   4区 5 街道，7社区
        String roleId = homeDao.getDicCodeByLoginName(loginName);
        Map<String, Object> map = new HashMap(8);
        if (StringUtils.isNotBlank(cropId)) {
            /**
             * 企业信息待审查数量
             */
            if (String.valueOf(Common.SIX).equals(roleId)) {
                map.put("QY", 0);
            } else {
                //状态（0保存，1提交审核（区审核中），2市审核通过，3审核不通过，4区审核通过（市审核中））
                String state = "1,4,5,6";
                //查询账号是区管理部门还是市管理部门3市   4区 5 街道，7社区
                String code = homeDao.getDicCodeByLoginName(loginName);
                String provinceCompany = null;
                //区
                if (String.valueOf(NumberEnum.FOUR.getNum()).equals(code)) {
                    provinceCompany = String.valueOf(NumberEnum.ONE.getNum());
                    //状态（0保存，1提交审核（区审核中），2市审核通过，3审核不通过，4区审核通过（市审核中））5社区审核中 6街道审核中
                    state = String.valueOf(NumberEnum.ONE.getNum());
                } else if (String.valueOf(NumberEnum.THREE.getNum()).equals(code)) {
                    //市
                    provinceCompany = String.valueOf(NumberEnum.ZERO.getNum());
                    //状态（0保存，1提交审核（区审核中），2市审核通过，3审核不通过，4区审核通过（市审核中））5社区审核中 6街道审核中
                    state = String.valueOf(NumberEnum.FOUR.getNum());
                } else if (String.valueOf(NumberEnum.FIVE.getNum()).equals(code)) {
                    state = String.valueOf(NumberEnum.SIX.getNum());
                } else if (String.valueOf(NumberEnum.SEVEN.getNum()).equals(code)) {
                    state = String.valueOf(NumberEnum.FIVE.getNum());
                }
                int countQY = homeDao.findCountByState(cropId, state, provinceCompany);
                map.put("QY", countQY);
            }
            /**
             * 教育情况
             */
            int jyNum = 0;
            if (Common.ADMIN.equals(loginName)) {
                jyNum = homeDao.getJyCount(cropId, "1,2,3");
            } else if (String.valueOf(Common.FOUR).equals(roleId)) {
                jyNum = homeDao.getJyCount(cropId, "2");
            } else if (String.valueOf(Common.SIX).equals(roleId)) {
                jyNum = homeDao.getJyCount(cropId, "3");
            } else if (String.valueOf(Common.THREE).equals(roleId)) {
                jyNum = homeDao.getJyCount(cropId, "1");
            }
            map.put("JY", jyNum);
            /**
             * 优良信息待审核数量
             */
            if (String.valueOf(Common.SIX).equals(roleId)) {
                map.put("YL", 0);
            } else {
                String state = excellentNegativeState(loginName);
                int ylNum = homeDao.findCorpDbsxNum(cropId, InfoTypeEnum.EXCELLENT.getCode(), state);
                int ylCount = homeDao.findCountByStateAndInfoType(cropId, InfoTypeEnum.EXCELLENT.getCode(), state);
                map.put("YL", ylNum + ylCount);
            }
            /**
             * 负面信息待审核数量
             */
            if (String.valueOf(Common.SIX).equals(roleId)) {
                map.put("FM", 0);
            } else {
                String state = excellentNegativeState(loginName);
                int fmNum = homeDao.findCorpDbsxNum(cropId, InfoTypeEnum.NEGATIVE.getCode(), state);
                int fmCount = homeDao.findCountByStateAndInfoType(cropId, InfoTypeEnum.NEGATIVE.getCode(), state);
                map.put("FM", fmNum + fmCount);
            }
            /**
             * 负面信息申诉待审核数量
             */
            if (String.valueOf(Common.SIX).equals(roleId)) {
                map.put("FMSS", 0);
            } else {
                String state = appealState(loginName);
                int fmssNum = homeDao.findAppealNum(cropId, InfoTypeEnum.NEGATIVE.getCode(), state, loginName);
                int fmssCount = homeDao.findAppealCount(cropId, InfoTypeEnum.NEGATIVE.getCode(), state, loginName);
                map.put("FMSS", fmssCount + fmssNum);
            }
            /**
             * 优良信息申诉待审核数量
             */
            if (String.valueOf(Common.SIX).equals(roleId)) {
                map.put("YLSS", 0);
            } else {
                String state = appealState(loginName);
                int fmssNum = homeDao.findAppealNum(cropId, InfoTypeEnum.EXCELLENT.getCode(), state, loginName);
                int fmssCount = homeDao.findAppealCount(cropId, InfoTypeEnum.EXCELLENT.getCode(), state, loginName);
                map.put("YLSS", fmssCount + fmssNum);
            }
            /**
             * 项目待办事项数量
             */
            if (String.valueOf(Common.SIX).equals(roleId)) {
                map.put("projectNum", 0);
            } else {
                //状态（0保存，1提交审核（区审核中），2市审核通过，3审核不通过，4区审核通过（市审核中））5社区审核中 6街道审核中
                String state = "1,4,5,6";
                //查询账号是区管理部门还是市管理部门3市   4区 5 街道，7社区
                String code = homeDao.getDicCodeByLoginName(loginName);
                if (String.valueOf(NumberEnum.THREE.getNum()).equals(code)) {
                    state = String.valueOf(NumberEnum.FOUR.getNum());
                } else if (String.valueOf(NumberEnum.FOUR.getNum()).equals(code)) {
                    state = String.valueOf(NumberEnum.ONE.getNum());
                } else if (String.valueOf(NumberEnum.FIVE.getNum()).equals(code)) {
                    state = String.valueOf(NumberEnum.SIX.getNum());
                } else if (String.valueOf(NumberEnum.SEVEN.getNum()).equals(code)) {
                    state = String.valueOf(NumberEnum.FIVE.getNum());
                }
                if (Common.ADMIN.equals(loginName)) {
                    cropId = "";
                }
                map.put("projectNum", homeDao.countPendingProjectNum(cropId, state));
            }
        }
        return map;
    }

    /**
     * 优良负面信息state
     *
     * @param loginName 登录名
     * @return 结果
     */
    private String excellentNegativeState(String loginName) {
        String dicCode = managerDao.getDicCodeByLoginName(loginName);
        String state;
        //3市管理部门,4区管理部门
        if (String.valueOf(NumberEnum.THREE.getNum()).equals(dicCode)) {
            state = String.valueOf(NumberEnum.NINE.getNum());
        } else if (String.valueOf(NumberEnum.FOUR.getNum()).equals(dicCode)) {
            state = "1,11";
        } else if (String.valueOf(NumberEnum.ONE_HUNDRED.getNum()).equals(dicCode)) {
            state = "1,9,11";
        } else {
            state = String.valueOf(NumberEnum.ONE_HUNDRED_EIGHTY_FOUR.getNum());
        }
        return state;
    }

    /**
     * 优良负面信息state
     *
     * @param loginName 登录名
     * @return 结果
     */
    private String appealState(String loginName) {
        String dicCode = managerDao.getDicCodeByLoginName(loginName);
        String state;
        if (String.valueOf(NumberEnum.FOUR.getNum()).equals(dicCode)) {
            state = "4,12";
        } else if (String.valueOf(NumberEnum.THREE.getNum()).equals(dicCode)) {
            state = String.valueOf(NumberEnum.TEN.getNum());
        } else if (String.valueOf(NumberEnum.ONE_HUNDRED.getNum()).equals(dicCode)) {
            state = "4,10,12";
        } else {
            state = String.valueOf(NumberEnum.ONE_HUNDRED_EIGHTY_FOUR.getNum());
        }
        return state;
    }

    /**
     * 获取登陆名
     *
     * @param loginName 登陆账号
     * @param loginType 登陆类型
     * @return 用户名
     */
    @Override
    public String getName(String loginName, String loginType) {
        String name = loginName;
        if (Common.ONE.equals(loginType)) {
            //获取物业企业的名字
            name = homeDao.getCorpName(loginName);
        } else if (Common.ZERO.equals(loginType)) {
            //获取管理部门名字
            name = homeDao.getManagerName(loginName);
        }
        return name;
    }

    /**
     * 获取柱状图
     *
     * @param id 登陆账号
     * @return 集合
     */
    @Override
    public List<Column> getColumnList(Integer id) {
        List<Column> columnList = new ArrayList<>();
        String dicCode = managerDao.getDicCodeById(id);
        if (StringUtils.isBlank(dicCode)) {
            return columnList;
        }
        //查询管理区域
        String area = homeDao.findAreaId(id);
        if (String.valueOf(NumberEnum.THREE.getNum()).equals(dicCode) || String.valueOf(NumberEnum.SIX.getNum()).equals(dicCode) || String.valueOf(NumberEnum.ONE_HUNDRED.getNum()).equals(dicCode)) {
            //如果是市主管部门或者协，查询各个区的信息
            columnList = homeDao.getColumnList();
        } else if (String.valueOf(NumberEnum.FOUR.getNum()).equals(dicCode) || String.valueOf(NumberEnum.FIVE.getNum()).equals(dicCode) || String.valueOf(NumberEnum.SEVEN.getNum()).equals(dicCode)) {
            //如果是区主管部门或者街道或社区，查询社区的信息
            columnList = homeDao.getDistrictColumnList(area);
        }
        for (Column column : columnList) {
            BigDecimal divide = new BigDecimal(column.getAllArea()).divide(new BigDecimal(10));
            column.setAllArea(String.valueOf(divide));
        }
        return columnList;
    }

    /**
     * 获取饼图
     *
     * @param year 年份
     * @param id   登陆ID
     * @return 集合
     */
    @Override
    public List<Pie> getPieList(String year, Integer id) {
        List<Pie> pieList = new ArrayList<>();
        String dicCode = managerDao.getDicCodeById(id);
        if (dicCode == null) {
            return pieList;
        }
        //查询管理区域
        String area = homeDao.findAreaId(id);
        if (String.valueOf(NumberEnum.THREE.getNum()).equals(dicCode) || String.valueOf(NumberEnum.SIX.getNum()).equals(dicCode) || String.valueOf(NumberEnum.ONE_HUNDRED.getNum()).equals(dicCode)) {
            //如果是市主管部门或者协，查询各个区的信息
            pieList = homeDao.getPieList(year);
        } else if (String.valueOf(NumberEnum.FOUR.getNum()).equals(dicCode) || String.valueOf(NumberEnum.FIVE.getNum()).equals(dicCode) || String.valueOf(NumberEnum.SEVEN.getNum()).equals(dicCode)) {
            //如果是区主管部门或者街道，查询街道的信息
            pieList = homeDao.getDistrictPieList(year, area);
        }
        return pieList;
    }

    /**
     * 查询在线企业列表
     *
     * @param id     登陆id
     * @param page   页面信息
     * @param online 是否在线（0否，1是）
     * @return 列表
     */
    @Override
    public ManagerPageResult getOnlineCorpList(Integer id, Page page, String online) {
        ManagerPageResult mr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        //查询管理区域
        String area = homeDao.findAreaId(id);
        Integer num = homeDao.getOnlineCorpNum(area, online);
        if (num <= Integer.parseInt(Common.ZERO)) {
            return mr;
        }
        List<Corp> onlineCorpList = homeDao.getOnlineCorpList(area, page, online);
        jr.setDs(onlineCorpList);
        mr.setJson(jr);
        mr.setCount(num);
        return mr;
    }

    /**
     * 查询在线人数
     *
     * @param id 登陆账号
     * @return 集合
     */
    @Override
    public List<Integer> online(Integer id) {
        List<Integer> list = new ArrayList<>();
        //查询管理区域
        String area = homeDao.findAreaId(id);
        if (StringUtils.isNotBlank(area)) {
            //当前在线人数
            int online = homeDao.getOnlineCorpNum(area, Common.ONE);
            //查询所有企业数量
            int qyNum = homeDao.getQyNum(area);
            list.add(online);
            list.add(qyNum);
        }
        return list;
    }
}
